<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('养修服务单项列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <!--			高级查询模块-->
        <div class="col-sm-12 search-collapse">
            <form id="appointment-form">
                <div class="select-list">
                    <ul>

                        <li>
                            客户姓名：
                            <input type="text" name="customerName"/>
                        </li>

                        <li>
                            联系方式：
                            <input type="text" name="customerPhone"/>
                        </li>
                        <li>
                            <label>预约状态：</label>
                            <select name="status"
                                    th:with="type = ${@dict.getType('cmi_status')}">
                                <option value="">所有</option>
                                <option th:each="dict:${type}" th:text="${dict.label}"
                                        th:value="${dict.value}">
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()">
                                <i class="fa fa-search"></i>&nbsp;
                                搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <!--			增删改模块-->
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()"
               shiro:hasPermission="business:appointment:add">
                <i class="fa fa-plus"></i> 添加
            </a>

        </div>
        <!--	        数据列表模块-->
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="common/include :: footer"/>
<script th:inline="javascript">
    // 查询是否有编辑权限
    var editFlag = [[${@permission.hasPermi('business:appointment:edit')}]];
    // 查询是否有删除权限
    var removeFlag = [[${@permission.hasPermi('business:appointment:remove')}]];
    // 到店点击权限
    var arrivalFlag = [[${@permission.hasPermi('business:appointment:arrival')}]];
    // 点击取消预约单权限：
    var cancelFlag = [[${@permission.hasPermi('business:appointment:cancel')}]];
    // 提交结算单权限
    var generateStatementFlag = [[${@permission.hasPermi('business:appointment:generateStatement')}]];

    // 查询数据字典服务类型
    var serviceType = [[${@dict.getType('cmi_service_type')}]];
    // 查询数据字典预约状态：
    var appointmentStatus = [[${@dict.getType('cmi_status')}]];
    var serviceTypeDatas = [[${@dict.getType('cmi_service_type')}]];
    var statusDatas = [[${@dict.getType('cmi_status')}]];
    var prefix = ctx + "/business/appointment";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            uniqueId: 'id',
            sortName: "status",
            modalName: "养修服务预约",
            columns: [{
                checkbox: true
            },
                {
                    field:'id',
                    title: ''
                },
                {
                    field: 'customerName',
                    title: '客户姓名',
                    sortable: true
                },
                {
                    field: 'customerPhone',
                    title: '客户联系方式',
                    sortable: true
                },
                {
                    field: 'appointmentTime',
                    title: '预约时间'
                },
                {
                    field: 'actualArrivalTime',
                    title: '实际到店时间'
                },
                {
                    field: 'licensePlate',
                    title: '车牌号码',
                    sortable: true
                },
                {
                    field: 'carSeries',
                    title: '汽车类型',
                    sortable: true
                }, {
                    field: 'serviceType',
                    title: '服务类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(serviceType, value);
                    }
                }, {
                    field: 'info',
                    title: '备注信息',
                }, {
                    field: 'status',
                    title: '状态',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(appointmentStatus, value);
                    }
                }, {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // 编辑按钮，有权限且预约单状态是在预约中才可以点击：
                        var editBtnFlag = editFlag + " " + (row.status > 0 ? "disabled" : "");
                        // 顾客到店确认按钮：只有预约中状态才可以点击
                        var arrivalBtnFlag = arrivalFlag + " " + (row.status > 0 ? "disabled" : "");
                        // 取消预约单：只有正在预约中状态才可以点击
                        var cancelBtnFlag = cancelFlag + " " + (row.status > 0 ? "disabled" : "");
                        // 删除预约单：只有预约中状态才可以删除
                        var removeBtnFlag = removeFlag + " " + (row.status > 0 ? "disabled" : "");
                        // 结算单按钮：当状态为已到店或结算单生成才可以用
                        var statementBtnFlag = generateStatementFlag + " " + ((row.status != 1 && row.status != 4) ? "disabled" : "");

                        actions.push('<a class="btn btn-success btn-xs ' + editBtnFlag + '" href="javascript:void(0)"' +
                            ' onclick="$.operate.edit(\'' + row.id + '\')">' +
                            '<i class="fa fa-edit"></i>编辑</a>');

                        actions.push('<a class="btn btn-warning btn-xs ' + arrivalBtnFlag + '" href="javascript:void(0)" ' +
                            'onclick="arrivalHandler(\'' + row.id + '\')">' +
                            '<i class="fa fa-sign-in"></i>到店</a> ');

                        actions.push('<a class="btn btn-primary btn-xs ' + statementBtnFlag + '" href="javascript:void(0)" ' +
                            'onclick="generateStatement(\'' + row.id + '\')"><i class="fa fa-remove"></i>结算单</a> ');
                        var more = [];
                        more.push("<a class='btn btn-default btn-xs " + cancelBtnFlag + "' href='javascript:void(0)' " +
                            "onclick='cancelHandler(" + row.id + ")'><i class='fa fa-reply'></i>取消</a> ");

                        more.push("<a class='btn btn-default btn-xs " + removeBtnFlag + "' href='javascript:void(0)' " +
                            "onclick='remove(" + row.id + ")'><i class='fa fa-remove'></i>删除</a>");

                        actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" ' +
                            'data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '">' +
                            '<i class="fa fa-chevron-circle-right"></i>更多操作</a>');

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 顾客到店确认点击事件
    function arrivalHandler(id) {
        $.modal.confirm("确认客户已经到店?", function () {
            $.operate.submit(prefix + "/arrival?id=" + id, "get");
        });
    }

    // 点击取消预约单：
    function cancelHandler(id) {
        $.modal.confirm("确定取消预约单吗?", function () {
            $.operate.submit(prefix + "/cancel?id=" + id, "get");
        });
    }

    // 点击删除预约单：
    function remove(id) {
        $.modal.confirm("确定删除预约单吗?", function () {
            $.operate.submit(prefix + "/remove?id=" + id, "get");
        });
    }

    // 点击预约单结算
    //结算单
    //预约单id
    function generateStatement(id) {
        //1：判断当前预约状态是否到店
        //如果到店---创建结算单----跳转editDetail.html
        //如果结算单已经生成---不需要创建---跳转editDetail.html/showDetail.html
        var row = $("#bootstrap-table").bootstrapTable("getRowByUniqueId", id);
        var msg = "确认生成结算单吗?";
        if (row.status == 4) {
            msg = "是否需要查看对应的结算单明细";
        }
        $.modal.confirm(msg, function () {
            //异步请求--返回结果---跳转
            //跳转controller 中执行 创建结算单逻辑---请求重定向转发到--showDetail.html/editDetail.html
            $.modal.openTab("结算单明细", prefix + "/generateStatement?id=" + id);
        });
    }
</script>
</body>
</html>